package com.hepinghospital.user.ui.activity.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.TeamListResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 团队问诊
 */

public class TeamListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView doctor_recyclerView;
    List<TeamListResponse> doctorList = new ArrayList<>();
    DoctorAdapter doctorAdapter;
    int diagType;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_team_list;
    }

    @Override
    protected void initViews() {
        tvTitle.setText("团队问诊");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        doctor_recyclerView.setLayoutManager(manager);
        doctorAdapter = new DoctorAdapter();
        doctor_recyclerView.setAdapter(doctorAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            doctorList.clear();
            initData();
        });
    }

    @Override
    protected void initData() {
        getTeamList();
    }

    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(TeamListActivity.this).inflate(R.layout.item_book_doctor_team, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString("id", doctorList.get(position).id + "");
                startActivity(TeamDetailActivity.class, bundle);
            });
            holder.tv_leader.setText("负责人:" + doctorList.get(position).leaderDoctorName + " " + doctorList.get(position).leaderDoctorTitle);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).goodAtNames);
            holder.tv_star.setText(doctorList.get(position).score);
            holder.tv_name.setText(doctorList.get(position).name);
            holder.tv_turn_num.setText(doctorList.get(position).num + "");
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(TeamListActivity.this, doctorList.get(position).thumbnailUrl, holder.img_head);

        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_leader, tv_skill, tv_star, tv_turn_num;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_leader = itemView.findViewById(R.id.tv_leader);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_star = itemView.findViewById(R.id.tv_star);
                tv_turn_num = itemView.findViewById(R.id.tv_turn_num);
                img_head = itemView.findViewById(R.id.img_head);
            }
        }
    }

    private void getTeamList() {
        RestProxy.getInstance().getTeamList(new BaseObserver<List<TeamListResponse>>() {
            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onSuccess(List<TeamListResponse> result) {
                doctorList.clear();
                doctorList.addAll(result);
                doctorAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
